import re

# 读取文件内容
file_path = 'd:/learn/pandas/input/中医内科主治医师考试题库及答案解析.txt'
with open(file_path, 'r', encoding='utf-8') as file:
    content = file.read()

# 定义正则表达式模式来匹配题目、选项、正确答案和答案解析
question_pattern = re.compile(
    r'(\d+\.\s.*?)\n([A-E]\..*?)(?=\d+\.|\Z)', re.DOTALL)
answer_pattern = re.compile(r'\[\正确答案\]\s([A-E])')
explanation_pattern = re.compile(r'\[\答案解析\]\s(.*?)(?=\d+\.|\Z)', re.DOTALL)

# 初始化结果列表
results = []

# 遍历匹配到的题目
for match in question_pattern.finditer(content):
    question = match.group(1).strip()
    options = match.group(2).strip().split('\n')
    answer_match = answer_pattern.search(match.group(0))
    answer = answer_match.group(1) if answer_match else None
    explanation_match = explanation_pattern.search(match.group(0))
    explanation = explanation_match.group(
        1).strip() if explanation_match else None

    # 将结果添加到列表中
    results.append({
        '题目': question,
        '选项': options,
        '正确答案': answer,
        '答案解析': explanation
    })

# 输出结果到新文件
output_file_path = 'd:/learn/pandas/input/中医内科主治医师考试题库及答案解析_整理版.txt'
with open(output_file_path, 'w', encoding='utf-8') as output_file:
    for result in results:
        output_file.write(f"题目: {result['题目']}\n")
        output_file.write("选项:\n")
        for option in result['选项']:
            output_file.write(f"  {option}\n")
        output_file.write(f"正确答案: {result['正确答案']}\n")
        output_file.write(f"答案解析: {result['答案解析']}\n")
        output_file.write("\n")

print(f"处理完成，结果已保存到 {output_file_path}")
